<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Upsanddowns\Controller;
use Think\Controller;
/**
 * 定时任务控制器
 */
class TimedTaskController extends Controller {


    /**
     * [auto_update 下注结算]
     * @return [type] [description]
     */
    public function auto_update(){
        $current_date=date('Ymd',time());
        $update_time=mktime(15,30,00,date('m'),date('d'),date('Y'));
        $rest=is_rest(date('Ymd'));
        if($rest[date('Ymd')]){
            echo '今天是节假日，未开市';exit;
        }
        if(time()<$update_time){
            echo '当日还未闭市，3点以后更新';exit;
        }
        $result=stock_info('000001');
        $data['rate']=$result['per'];
        $rate_type = ($result['per']>0)?1:2;

        $periods = M('RoseActivity')->where('status=1')->order('periods_start_time desc')->getField('periods');

        $where = array();
        $where['periods'] = $periods;   //加个期数判断
        $where['date'] = $current_date;
        $where['profit_gold_bean'] = 0;
        $bet_list=M('rose_buy')->where($where)->select();
        foreach($bet_list as $key=>$val){
            if($val['type']==$rate_type){ //猜中
                 $profit=round($val['gold_bean']*$val['odds']);
                $profit_gold_bean=$profit-$val['gold_bean'];
                M('rose_buy')->where("id=".$val['id'])->setInc('profit_gold_bean',$profit_gold_bean);
                //发送豆子
                $other=array('user_id'=>$val['user_id'],'rose_activity_id'=>$val['rose_activity_id'],'rose_buy_id'=>$val['id'],'type'=>2,'log_info'=>'');
                //减少用户帐户中的金豆并记录日志
                modify_account(array('gold_bean'=>$profit),$other);
            }else{  //未猜中
                $profit_gold_bean=$val['gold_bean'];
                M('rose_buy')->where("id=".$val['id'])->setDec('profit_gold_bean',$profit_gold_bean);
            }
        }
    }
    /**
     * 获取曲线图数据
     */
    public function get_odds_data(){
        //获取当前下注活动信息
        //$result=M('rose_activity')->where("time=".get_now_time())->find();
        $result=M('rose_activity')->where("status=1")->order('periods_start_time desc')->find();
        $time=mktime(13,00,00,date('m'),date('d'),date('Y'));
        //13:00--13:01执行一次，把数据清空并把涨赔率都设为2
        $data=F('odds');
        if(time()>=$time && time()-$time<60){
            F('odds','');
            $data=array();
            $data['rise_odds'][time()]=2;//涨赔率
            $data['fall_odds'][time()]=2;//跌赔率
        }else{
            $personsAndOdds = $this->get_beans_and_odds($result['periods']); //获取赔率和人数
            $data['rise_odds'][time()]=$personsAndOdds['rise_odds'];//涨赔率
            $data['fall_odds'][time()]=$personsAndOdds['fall_odds'];//跌赔率
        }
        F('odds',$data);
    }

    public function get_beans_and_odds($periods){
        //赔率、金豆数 可以根据rose_buy 表中的数据 进行实时计算
        $info = array();
        $current_date = get_now_time();
        //获取当日赔率
        $where = array();
        $where['profit_gold_bean'] = 0;
        $where['periods'] = $periods;
        $where['date'] = $current_date;
        $buys = M('RoseBuy')->field('type,sum(gold_bean) as current_date_total_bean')->where($where)->group('type')->select();
        foreach($buys as $buy){
            if($buy['type']==1){    //1位涨
                $rise_gold_bean=$buy['current_date_total_bean'];//买涨的总的金豆数
                $info['rise_gold_bean'] = $rise_gold_bean;
            }elseif($buy['type']==2){
                $fall_gold_bean=$buy['current_date_total_bean'];//买跌的总的金豆数
                $info['fall_gold_bean'] = $fall_gold_bean;
            }
        }
        $fictitious_data=F('fictitious_data');//虚拟金豆和人数
        $info['rise_gold_bean']=$rise_gold_bean=$rise_gold_bean+$fictitious_data['rise_bean'];//加上虚拟金豆
        $info['fall_gold_bean']=$fall_gold_bean=$fall_gold_bean+$fictitious_data['fall_bean'];
        if(!$rise_gold_bean || !$fall_gold_bean){
            $info['rise_odds'] = 2;
            $info['fall_odds'] = 2;
        }else{
            $info['rise_odds'] = sprintf("%.2f",($rise_gold_bean+$fall_gold_bean)/$rise_gold_bean);
            $info['fall_odds'] = sprintf("%.2f",($rise_gold_bean+$fall_gold_bean)/$fall_gold_bean);
        }
        //限制赔率最大为5
        $info['rise_odds']=$info['rise_odds']>"5.0"?"5.0":$info['rise_odds'];
        $info['fall_odds']=$info['fall_odds']>"5.0"?"5.0":$info['fall_odds'];
        return $info;
    }


}



